<?php
namespace common\models\trade;
use common\models\BaseModel;
use common\models\trade\TradeUpgradeModel;
use common\models\trade\TradeSpeedBonusModel;
use common\models\trade\TradeTeamBonusModel;
/*
	交易模式

*/

class TradeModel extends BaseModel
{

	public static function tableName()
	{
		return '{{%trade_model}}';
	}

	public function rules()
	{
		return [
			[['model_code','model_name','model_desc','model_condition','assign_type'],'string'],
			[['mch_id','sort','model_id'],'integer'],
			[['model_code'],'required'],
		];
	}

	## 根据触发事件获取配置
	public static function getTriggerEvent($triggerType)
	{
		$data = self::find()->where(['mch_id'=>MCHID])
					->andWhere(['like','trigger_type',$triggerType.','])
					->orderBy('priority asc')
					->asarray()->all();
		return $data;
	}	


	## 查询分享模式配置信息
	public static function getRecommendConf($code)
	{
		$where = ['a.model_code'=>$code,'a.mch_id'=>MCHID];
		$data  = self::find()->select(['a.*','b.*'])->alias('a')
					->leftJoin('trade_recommend as b','a.model_id = b.model_id')
					->where($where)
					->asarray()->one();
		return $data;

	}

	## 会员升级模式配置
	public static function getUpgradeConf($code)
	{
		$where = ['model_code'=>$code,'mch_id'=>MCHID];		
		$data  = tradeUpgradeModel::find()->select(['a.*','b.*'])->alias('a')
					->leftJoin('trade_model as b','a.model_id = b.model_id')
					->where($where)
					->orderBy('grade asc')
					->asarray()->all();
		return $data;		
	}

	## 获取静态释放配置
	public static function getStaticReleaseConf($code)
	{
		$where = ['a.model_code'=>$code,'a.mch_id'=>MCHID];
		$data  = self::find()->select(['a.*','b.*'])->alias('a')
					->leftJoin('trade_static_release as b','a.model_id = b.model_id')
					->where($where)
					->asarray()->one();
		return $data;		
	}


	## 获取加速释放配置
	public static function getSpeedBonusConf($code)
	{
		$where = ['model_code'=>$code,'mch_id'=>MCHID];		
		$data  = TradeSpeedBonusModel::find()->select(['a.*','b.*'])->alias('a')
					->leftJoin('trade_model as b','a.model_id = b.model_id')
					->where($where)
					->orderBy('level asc')
					->asarray()->all();
		return $data;	
	}

	## 获取团队奖配置 
	public static function getTeamBonusConf($code)
	{
		$where = ['model_code'=>$code,'mch_id'=>MCHID];		
		$data  = tradeTeamBonusModel::find()->select(['a.*','b.*'])->alias('a')
					->leftJoin('trade_model as b','a.model_id = b.model_id')
					->where($where)
					->orderBy('grade asc')
					->asarray()->all();
		return $data;	
	}


}